<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-10
 * Time: 21:09
 */

namespace app\wx\controller;


use app\tools\Curl;
use app\tools\MIniprogram;
use app\tools\Wx;
use think\Controller;

class Index
{
    function __construct()
    {
        error_reporting(0);
    }

    function index(){

        /*//        验证 token

        $nonce     = $_GET['nonce'];
        $token     = 'token123876';
        $timestamp = $_GET['timestamp'];
        $echostr   = $_GET['echostr'];
        $signature = $_GET['signature'];
        //形成数组，然后按字典序排序
        $array = array();
        $array = array($nonce, $timestamp, $token);
        sort($array);
        //拼接成字符串,sha1加密 ，然后与signature进行校验
        $str = sha1( implode( $array ) );
        if( $str == $signature && $echostr ){
            //第一次接入weixin api接口的时候
            echo  $echostr;
            exit;
        }*/

        $data = file_get_contents("php://input");
//        file_put_contents("1.txt", $data);

        $ob = (array)simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);

        if($ob['MsgType'] == "event" && $ob['Event'] == "subscribe"){

            $wx = new Wx();

            $openid = $ob['FromUserName'];

            $user_info = $wx->getUserInfo($openid);

            $user_info['type'] = 2;

            try{
                db('customer')->insert($user_info);
            }catch (\Exception $exception){
//                echo $exception->getMessage();
            }

            $msg = [
                'ToUserName'=> $openid,
                'FromUserName'=> $ob['ToUserName'],
                'CreateTime'=> time(),
                'MsgType'=> 'text',
                'Content'=> '你终于关注我啦！
“虎猫”送福利啦，空调清洗免费上门服务，活动详情请联系客服哦！
',
            ];

//            exit(array2xml($msg));
            echo array2xml($msg);
            $wx = new Wx();
//            $wx->uploadImg("/home/cat/public/2.jpeg");

            $data = [
                'touser'=> $openid,
                'msgtype'=> "miniprogrampage",
                "miniprogrampage"=>[
                    'title'=> '找维修，找安装，找工作，查快递',
                    'appid'=> 'wx9cf61f67ed5d5321',
                    'pagepath'=> 'pages/new_index/index',
                    'thumb_media_id'=> 'ZjgL53Qfi4FBL1WebXSsnc891gmwlnH4IU6OPM3p1ZA'
                ]
            ];
            $wx->sendMsg($data);

        }elseif ($ob['MsgType'] == "text"){

            if($ob['Content'] == '报告')
            {
                $msg = [
                    "ToUserName"=> $ob['FromUserName'],
                    "FromUserName"=> $ob['ToUserName'],
                    "CreateTime"=> time(),
                    "MsgType"=> 'news',
                    "ArticleCount"=> 1,
                    "Articles"=> [
                        'item'=> [
                            'Title'=> '您的面相报告',
                            'Description'=> '',
                            'PicUrl'=> 'https://cat.legu17.com/uploads/20190411/02f684ad3077e1ffd83bed1ee56d81c3.png',
                            'Url'=> 'http://wx.scriptchao.xyz/app/./index.php?i=4178&c=entry&eid=1369',
                        ]
                    ],
                ];

                exit(array2xml($msg));
            }
        }

    }

    function createMenu()
    {
        $wx = new Wx();
        $data = $wx->uploadImg("/home/www/cat/public/5.jpeg");
        $data2 = $wx->uploadImg("/home/www/cat/public/3.jpeg");
//        dump($data);
        $data = [
            'button'=> [
                [
                    'name'=> '服务',
                    'sub_button'=> [
                        [
                            'type'=> 'miniprogram',
                            'name'=> '找工作',
                            'appid'=> 'wx9cf61f67ed5d5321',
                            'pagepath'=> 'pages/new_index/index',
                            'url'=> 'http://wx.scriptchao.xyz/app/./index.php?i=4178&c=entry&eid=1369'
                        ],
                        [
                            'type'=> 'miniprogram',
                            'name'=> '开锁',
                            'appid'=> 'wx9cf61f67ed5d5321',
                            'pagepath'=> 'pages/business/list?id=1',
                            'url'=> 'http://wx.scriptchao.xyz/app/./index.php?i=4178&c=entry&eid=1369'
                        ],
                        [
                            'type'=> 'miniprogram',
                            'name'=> '维修/安装',
                            'appid'=> 'wx9cf61f67ed5d5321',
                            'pagepath'=> 'pages/repair/openRepair/openRepair',
                            'url'=> 'http://wx.scriptchao.xyz/app/./index.php?i=4178&c=entry&eid=1369'
                        ],
                        [
                            'type'=> 'miniprogram',
                            'name'=> '家电清洗',
                            'appid'=> 'wx9cf61f67ed5d5321',
                            'pagepath'=> 'pages/repair/openRepair/openRepair',
                            'url'=> 'http://wx.scriptchao.xyz/app/./index.php?i=4178&c=entry&eid=1369'
                        ],
                    ]
                ],
                [
                    "type"=> "media_id",
                    "name"=> "推广海报",
                    "media_id"=> $data
                ],
                [
                   "type"=> "media_id",
                   "name"=> "联系客服",
                   "media_id"=> $data2
                ]
            ]
        ];

        $wechat = config('wx');
        $access_token = MIniprogram::accessToken($wechat);
        echo 1231231;
        echo Curl::curl("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token, json_encode($data, JSON_UNESCAPED_UNICODE));
    }



}